package pt.cp.view;

import model.KeyEntity;
import pt.cp.model.Station;
import swing.FavoriteButton;

import javax.swing.*;
import java.awt.*;
import java.util.Observable;
import java.util.Observer;

import static pt.cp.CP.STATIONS;
import static swing.Dialog.Flag.CENTERED;
import static swing.Dialog.Flag.PACKED;

/**
 * Created by nuno-rafael-figueiredo on 09-03-2014.
 */
public class StationPicker extends JButton {
    private Station station;

    public StationPicker() {
        super("Pick");
        addActionListener(actionEvent -> new Dialog().setVisible(true));
    }

    public Station getStation() {
        return station;
    }

    private class Dialog extends swing.Dialog {
        Dialog() {
            super(PACKED, CENTERED);
            JTabbedPane tabbedPane = new JTabbedPane();
            add(tabbedPane);
            tabbedPane.add("Preferred", new PreferredTab());
            tabbedPane.add("Most used", new MostUsedTab());
            tabbedPane.add("All", new AllTab());
        }

        class PreferredTab extends Tab {
            PreferredTab() {
                super(STATIONS.getFavorites());
            }
        }

        class MostUsedTab extends Tab {
            MostUsedTab() {
                super(STATIONS.getMostUsed());
            }
        }

        class AllTab extends Tab {
            AllTab() {
                super(STATIONS);
            }
        }

        class Tab extends JScrollPane implements Observer {
            private static final int GRID_COLUMNS = 4;
            private final JPanel gridPanel = new JPanel(new GridLayout(0, GRID_COLUMNS));

            private final KeyEntity<String, Station> stations;

            Tab(KeyEntity<String, Station> stations) {
                setViewportView(gridPanel);

                this.stations = stations;
                stations.addObserver(this);
                update(stations, null);
            }

            @Override
            public void update(Observable observable, Object o) {
                gridPanel.removeAll();

                for (Station station : stations.values())
                    gridPanel.add(new StationComponent(station));
            }

            class StationComponent extends Box {
                private final FavoriteButton favoriteButton;

                public StationComponent(Station station) {
                    super(BoxLayout.LINE_AXIS);
                    favoriteButton = new FavoriteButton<>(STATIONS, station);
                    add(favoriteButton);
                    add(new StationButton(station));
                }

                class StationButton extends JButton {

                    public static final int MINIMUM_WIDTH = 150;

                    public StationButton(final Station station) {
                        super(station.toString());
                        favoriteButton.setMinimumSize(new Dimension(MINIMUM_WIDTH, favoriteButton.getHeight()));
                        addActionListener(actionEvent -> {
                            StationPicker.this.station = station;
                            Dialog.this.setVisible(false);
                            StationPicker.this.setText(station.toString());
                        });
                    }
                }
            }
        }
    }
}
